﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Controls;
using CharacterTrailClient.Query.Item;
using CharacterTrailClient.Windows.Exceptions;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Windows
{
    public partial class FormRepairMaintain : FormBase, IRepairHeaderViewControl
    {
        private readonly List<TRepairHeaderModel> _repairHeaderModels = new List<TRepairHeaderModel>();
        private readonly IRepair _repairImpl = InstanceFactory.GetRepairImpl();
        public FormRepairMaintain()
        {
            InitializeComponent();
            this.productBaseRepairEdit1.FRepairHeaderViewControl = this;
            this.gridControlRepairHeaderModels.DataSource = _repairHeaderModels;
            this.gridViewQuery.Columns.ColumnByFieldName("RepairNote").ColumnEdit = base.RepMemoEdit;
        }

        public override string HeaderText
        {
            get
            {
                return "   返修维护   ";
            }
        }

        private void CmdQuery1_Click(object sender, EventArgs e)
        {
            string conditon = this.dateItemInputQuery.GetCondition();

            ClearProductDetailModels();
            var list = _repairImpl.GetRepairHeaderModelsByCondition( " 90=90 " + conditon);
            AppendRepairHeaderModels(list);
        }

        public void AppendRepairHeaderModel(TRepairHeaderModel repairHeaderModels)
        {
            _repairHeaderModels.Add(repairHeaderModels);
            this.gridControlRepairHeaderModels.RefreshDataSource();
        }

        public void ClearProductDetailModels()
        {
            _repairHeaderModels.Clear();
            this.gridControlRepairHeaderModels.RefreshDataSource();
        }

        public void AppendRepairHeaderModels(List<TRepairHeaderModel> repairHeaderModels)
        {
            foreach (var repairHeaderModel in repairHeaderModels)
            {
                _repairHeaderModels.Add(repairHeaderModel);
            }
            this.gridControlRepairHeaderModels.RefreshDataSource();
        }

        private void QueryData()
        {
            try
            {
                string conditon = "  24 =24 ";
                foreach (Control control in this.groupControlRepair.Controls)
                {
                    if (control is IQueryItem)
                    {
                        var queryItem = (IQueryItem)control;
                        conditon += queryItem.GetCondition();
                    }
                }
                var list = _repairImpl.GetVRepairBaseModelsByCondition(conditon);
                this.gridControlQuery.DataSource = list;
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        /// <summary>
        /// 查询维修记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            QueryData();
        }
    }
}